package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.StringUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizSeckillPage;
import com.member.shop.service.ISysBizSeckillPageService;
import com.member.shop.vo.SysBizSeckillPageExport;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【秒杀页】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2025-04-12
 **/
@RestController
@RequestMapping("/shop/seckill")
public class SysBizSeckillPageController extends BaseController {
    @Resource
    private ISysBizSeckillPageService sysBizSeckillPageService;

    /**
     * 查询秒杀页列表
     */
    @PreAuthorize("@ss.hasPermi('shop:seckill:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizSeckillPage sysBizSeckillPage) {
        startPage();
        LambdaQueryWrapper<SysBizSeckillPage> queryWrapper = new LambdaQueryWrapper<SysBizSeckillPage>();
        queryWrapper.eq(sysBizSeckillPage.getTenantId()!=null && sysBizSeckillPage.getTenantId()!=0,SysBizSeckillPage::getTenantId, sysBizSeckillPage.getTenantId());
        queryWrapper.like(StringUtils.isNotEmpty(sysBizSeckillPage.getTitle()),SysBizSeckillPage::getTitle, sysBizSeckillPage.getTitle());
        queryWrapper.eq(sysBizSeckillPage.getDisplayed()!=null,SysBizSeckillPage::getDisplayed, sysBizSeckillPage.getDisplayed());
        queryWrapper.orderByDesc(SysBizSeckillPage::getId);
        List<SysBizSeckillPage> list = sysBizSeckillPageService.selectSysBizSeckillPageList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出秒杀页列表
     */
    @PreAuthorize("@ss.hasPermi('shop:seckill:export')")
    @Log(title = "秒杀页", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizSeckillPage sysBizSeckillPage) {
        List<SysBizSeckillPageExport> list = sysBizSeckillPageService.export(sysBizSeckillPage);
        ExcelUtil<SysBizSeckillPageExport> util = new ExcelUtil<SysBizSeckillPageExport>(SysBizSeckillPageExport.class);
        util.exportExcel(response, list, "秒杀页数据");
    }

    /**
     * 获取秒杀页详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:seckill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizSeckillPageService.selectSysBizSeckillPageById(id));
    }

    /**
     * 新增秒杀页
     */
    @PreAuthorize("@ss.hasPermi('shop:seckill:add')")
    @Log(title = "秒杀页", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizSeckillPage sysBizSeckillPage) {

        List<SysBizSeckillPage> sysBizSeckillPages = sysBizSeckillPageService.selectSysBizSeckillPageList(
                new LambdaQueryWrapper<SysBizSeckillPage>()
                        .eq(SysBizSeckillPage::getTenantId, sysBizSeckillPage.getTenantId())
                        .eq(SysBizSeckillPage::getDisplayed, 0)
                        .eq(SysBizSeckillPage::getDeleted, 0)
        );

        if(CollectionUtils.isNotEmpty(sysBizSeckillPages) && sysBizSeckillPage.getDisplayed()==0){
            return AjaxResult.error("一个站点同时只能上架一个秒杀活动");
        }

        return toAjax(sysBizSeckillPageService.insertSysBizSeckillPage(sysBizSeckillPage));
    }

    /**
     * 修改秒杀页
     */
    @PreAuthorize("@ss.hasPermi('shop:seckill:edit')")
    @Log(title = "秒杀页", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizSeckillPage sysBizSeckillPage) {
        List<SysBizSeckillPage> sysBizSeckillPages = sysBizSeckillPageService.selectSysBizSeckillPageList(
                new LambdaQueryWrapper<SysBizSeckillPage>()
                        .eq(SysBizSeckillPage::getTenantId, sysBizSeckillPage.getTenantId())
                        .eq(SysBizSeckillPage::getDisplayed, 0)
                        .eq(SysBizSeckillPage::getDeleted, 0)
                        .ne(SysBizSeckillPage::getId, sysBizSeckillPage.getId())
        );

        if(CollectionUtils.isNotEmpty(sysBizSeckillPages) && sysBizSeckillPage.getDisplayed()==0){
            return AjaxResult.error("一个站点同时只能上架一个秒杀活动");
        }
        return toAjax(sysBizSeckillPageService.updateSysBizSeckillPage(sysBizSeckillPage));
    }

    /**
     * 删除秒杀页
     */
    @PreAuthorize("@ss.hasPermi('shop:seckill:remove')")
    @Log(title = "秒杀页", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizSeckillPageService.deleteSysBizSeckillPageByIds(ids));
    }
}
